package com.itheima.stargraph.config;

import com.itheima.stargraph.api.ComfyuiApi;
import com.itheima.stargraph.handler.ComfyuiMessageHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.client.WebSocketClient;
import org.springframework.web.socket.client.WebSocketConnectionManager;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;

/**
 * Comfyui配置类
 */
@Configuration
public class ComfyuiConfig {


    @Bean
    public ComfyuiApi comfyuiApi() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://localhost:8188")
                .addConverterFactory(JacksonConverterFactory.create())
                .build();
        return retrofit.create(ComfyuiApi.class);
    }

    @Bean
    public WebSocketConnectionManager webSocketConnectionManager(ComfyuiMessageHandler comfyuiMessageHandler) {
        WebSocketClient webSocketClient = new StandardWebSocketClient();
        String url = "ws://localhost:8188/ws?clientId=star-graph";
        WebSocketConnectionManager webSocketConnectionManager = new WebSocketConnectionManager(webSocketClient,comfyuiMessageHandler,url);
        webSocketConnectionManager.start();
        return webSocketConnectionManager;
    }
}